<template>
	<view class="content">
		<view class="statistics">
			<view class="left" @click="ordinary(0)">
				<button class='but' hover-class='none'>普通助力</button>
			</view>
			<view class="right" @click="ordinary(1)">
				<button class='but' hover-class='none'>申请设备助力</button>
			</view>
		</view>
		<uniModel :textmsg="textmsg" v-show="showTextmsg" @operation="operation"></uniModel>
	</view>
</template>
<script>
	import IndexSwiper from '../../compnents/indexSwiper.vue'
	import uniModel from '../../compnents/unimodel.vue'
	import { baseURL } from "@/utils/req.js"
	import {
		identity
	} from "../../../common/verifyLogin.js"
	import {
		addUserApi,
		getHelper
	} from "../../api/add_user.js"
	export default {
		components: {
			IndexSwiper,
			uniModel
		},
		data() {
			return {
				nickName: '',
				memberId: '',
				open_id: '',
				headimage: '',
				showTextmsg:false,
				textmsg: {
					title: '',
					content: '',
					confirm: ''
				}


			}
		},
		computed: {

		},
		onLoad(option) {
			// console.log(6666,option)
			uni.setStorageSync('member_id', option.member_id)
			this.$forceUpdate();
			if (!identity()) {
				uni.navigateTo({
					url: "../authorization/authorization",
				})
			} else {
				this.authorizedLog();
			}

		},
		onShow() {
			this.memberId = uni.getStorageSync('member_id');
			this.open_id = uni.getStorageSync('openId');
			this.nickName = uni.getStorageSync('nickName');
			this.headimage = uni.getStorageSync('avatarUrl');
			// console.log("会员"+this.memberId)
			// console.log("open_id"+this.open_id)
			// console.log("昵称"+this.nickName)
			// console.log("头像"+this.headimage)


		},
		methods: {
			//确认框
			operation(e) {
				let that = this
					that.showTextmsg = false	
			},
			authorizedLog() {
				wx.login({
					success: (res) => {
						// console.log(8888,res)
						var code = res.code;
						this.authlogin(code)
					}
				})
			},
			authlogin(code) {
				let _this = this
				let params = {
					jsCode: code
				}
				uni.showLoading({
					title: "正在加载中...."
				})
				let sessionKey = uni.getStorageSync('sessionKey')
				uni.request({
					// url: "https://mlpay.aoe1.top" + '/user/wechat/jscode2session',//线上地址
					url: baseURL + '/user/wechat/jscode2session',//本地地址
					method: 'POST',
					data: params,
					header: {
						'content-type': 'application/json;charset=UTF-8',
						'X-HXCharge-Authentication': '' + sessionKey
					},
					success: function(res) {
						// console.log(666,res)
						if (res.statusCode == 200) {
							uni.hideLoading()
							_this.getUserInfo();
							uni.setStorageSync("wxLogin", true);
							uni.setStorageSync('openId', res.data.data.openId);
							uni.setStorageSync('sessionKey', res.data.data.sessionKey);
						}

					}
				})
			},
			getUserInfo() {
				uni.showModal({
					title: '温馨提示',
					content: '亲，授权微信登录后才能正常使用小程序功能',
					success(res) {
						console.log(0)
						console.log(res)
						//如果用户点击了确定按钮
						if (res.confirm) {
							uni.getUserProfile({
								desc: '获取你的昵称、头像、地区及性别',
								success: res => {
									// console.log(res.userInfo.nickName);
									// console.log(res.userInfo.avatarUrl);
									uni.setStorageSync("nickName", res.userInfo.nickName)
									uni.setStorageSync("avatarUrl", res.userInfo.avatarUrl)
									uni.navigateTo({
										url: '../pages/assistance/assistance'
									})
								},
								fail: res => {
									//拒绝授权
									uni.showToast({
										title: '您拒绝了请求,不能正常使用小程序',
										icon: 'error',
										duration: 1500
									});
									return;
								}
							});
						} else if (res.cancel) {
							//如果用户点击了取消按钮
							// console.log(3);
							uni.showToast({
								title: '您拒绝了请求,不能正常使用小程序',
								icon: 'error',
								duration: 1500
							});
							return;
						}
					}
				});
			},
			async ordinary(type) {

				//验证表单
				if (this.nickName == '' || this.nickName == null || this.nickName == undefined) {
					uni.showToast({
						title: '用户名为空',
						icon: 'error',
						duration: 1500
					});
					return;
				}
				if (this.memberId == '' || this.memberId == null || this.memberId == undefined) {
					uni.showToast({
						title: '会员信息为空',
						icon: 'error',
						duration: 1500
					});
					return;
				}
				if (this.open_id == '' || this.open_id == null || this.open_id == undefined) {
					uni.showToast({
						title: 'openid为空',
						icon: 'error',
						duration: 1500
					});
					return;
				}
				if (this.headimage == '' || this.headimage == null || this.headimage == undefined) {
					uni.showToast({
						title: '头像为空',
						icon: 'error',
						duration: 1500
					});
					return;
				}
				this.getHelper(type);
				// const res = await addUserApi(params)
				// console.log(8888, res)
				// if (res.statusCode === 200) {

				// }
			},
			async getHelper(type) {
				let params = {
					helpType: type,
					nickName: this.nickName,
					memberId: this.memberId,
					openId: this.open_id,
					headimgurl: this.headimage
				}
				const res = await getHelper(params)
				if (res.statusCode == 200) {
					if (type == 0 && res.data.code == '0') {
						this.showTextmsg=true
						this.textmsg.title=res.data.message
						//判断助力成功
					} else {
					this.showTextmsg=true
					this.textmsg.title=res.data.message
					}
					if (type == 1 && res.data.code == '0') {
						this.toEquipment()
					} else {
					this.showTextmsg=true
					this.textmsg.title=res.data.message
					}

				}

			},
			toEquipment() {
				uni.navigateTo({
					url: '../add_user_info/add_user_info'
				})
			}






		}
	}
</script>
<style>
	page {
		background-color: #ffffff;
		background: url(https://wanxian-test.oss-cn-shenzhen.aliyuncs.com/32062cdf602327c14d911b0096477d4.jpg);
		background-size: 100%;

	}
</style>
<style lang="scss" scoped>
	.content {
		text-align: center;
		align-items: center;

		// padding-bottom: 140rpx;
		.statistics {
			margin-left: 32rpx;
			margin-right: 32rpx;
			height: 100rpx;
			text-align: center;
			justify-content: space-between;
			display: flex;
			position: fixed;
			bottom: 160rpx;
			right: 0;
			left: 0;

			.left {
				height: 100rpx;
				text-align: center;
				line-height: 100rpx;

				.but {
					background-image: url('https://jialianxin-1301053941.cos.ap-chengdu.myqcloud.com/static/buy/05.png');
					background-size: 100% 100%;
					width: 350rpx;
					height: 140rpx;
					font-size: 1.6em;
					color: white;
					background-color: rgba(255, 255, 255, 0);
				}

				.but:after {
					border: none;
				}


			}

			.right {
				height: 100rpx;
				text-align: center;
				line-height: 100rpx;

				.but {
					background-image: url('https://jialianxin-1301053941.cos.ap-chengdu.myqcloud.com/static/buy/05.png');
					background-size: 100% 100%;
					width: 350rpx;
					height: 140rpx;
					font-size: 1.6em;
					color: white;
					background-color: rgba(255, 255, 255, 0);
				}

				.but:after {
					border: none;
				}
			}
		}





	}
</style>
